/*
 * @Author: your name
 * @Date: 2022-03-31 21:34:54
 * @LastEditTime: 2023-07-21 15:57:25
 * @LastEditors: YNT 2501512800@qq.com
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \22-03-19 智慧社区\pages\index\components\consult\index.js
 */
// pages/index/components/consult/index.js
import SpecialModel from '../../../../api/special'
import dayjs from '../../../../utils/dayjs'

Component({
  options: {
    styleIsolation: 'page-isolated',
  },
  data: {
    nvabarData: {
      navigationBarTextStyle: 'black', // 胶囊主题 white || black
      navigationBarBackground: '#FEFEFE', // 导航栏背景色
      navigationBarTitleText: '邻里资讯',
      hideCapsule: true,
    },
    TypeList: [], //列表数组

    List: [], //邻里公约

    page: 1,
    totalPage: 2,
    news_type_id: '',
    scrollTop: 0,
    scrollHeight: 0,
  },

  lifetimes: {
    attached: function () {
      let _that = this

      wx.showLoading()

      SpecialModel.newsType()
        .then(({ data }) => {
          _that.setData({
            TypeList: data,
            news_type_id: data[0].id,
          })
        })
        .catch(({ errCode, data }) => {})
        .finally(() => {
          wx.hideLoading()
          this.getList()
        })
    },
  },

  methods: {
    onChange(e) {
      let news_type_id = ''
      if (e.detail.index < 5) {
        news_type_id = e.detail.index + 1
      } else {
        news_type_id = e.detail.index + 2
      }

      this.setData({
        news_type_id: news_type_id,
        page: 1,
        totalPage: 2,
        List: [],
      })

      this.getList()
    },

    getList: function () {
      let _that = this

      let List = _that.data.List
      let page = _that.data.page
      let totalPage = _that.data.totalPage
      let news_type_id = _that.data.news_type_id
      if (page < totalPage) {
        wx.showLoading()
        SpecialModel.newsList({
          news_type_id: news_type_id,
          page: page,
          per_page: 6,
        })
          .then(({ data }) => {
            data.items.forEach((item) => {
              item.create_time = dayjs(item.create_time).format(
                'YYYY-MM-DD HH:mm'
              )
            })
            for (var i = 0; i < data.items.length; i++) {
              List.push(data.items[i])
            }
            _that.setData({
              List,
              page: data.next_num ? data.next_num : data.pages,
              totalPage: data.pages,
            })
          })
          .catch(({ errCode, data }) => {
            _that.setData({
              List: [],
            })
          })
          .finally(() => {
            wx.hideLoading()
          })
      }
    },

    // 滑动到底部
    bindDownLoad() {
      this.getList()
    },

    scroll(event) {
      // this.setData({
      //   scrollTop:event.detail.scrollTop
      // })
    },

    // 滑动到顶部
    topLoad(event) {},
  },
})

